package com.xiaoyu.base.view.list.util.diff;

import androidx.recyclerview.widget.DiffUtil;

import java.util.List;

public abstract class BaseDiffCallback<T> extends DiffUtil.Callback {

    protected final List<T> mOldList;
    protected final List<T> mNewList;

    public BaseDiffCallback(List<T> oldList, List<T> newList) {
        this.mOldList = oldList;
        this.mNewList = newList;
    }

    @Override
    public int getOldListSize() {
        return mOldList.size();
    }

    @Override
    public int getNewListSize() {
        return mNewList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        if (oldItemPosition >= mOldList.size()) {
            return false;
        }
        T oldItem = mOldList.get(oldItemPosition);
        if (newItemPosition >= mNewList.size()) {
            return false;
        }
        T newItem = mNewList.get(newItemPosition);
        return isSameItem(oldItem, newItem);
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        if (oldItemPosition >= mOldList.size()) {
            return false;
        }
        T oldItem = mOldList.get(oldItemPosition);
        if (newItemPosition >= mNewList.size()) {
            return false;
        }
        T newItem = mNewList.get(newItemPosition);
        return isSameItemContent(oldItem, newItem);
    }

    protected abstract boolean isSameItem(T oldItem, T newItem);

    protected abstract boolean isSameItemContent(T oldItem, T newItem);

    public List<T> getOldList() {
        return mOldList;
    }

    public List<T> getNewList() {
        return mNewList;
    }
}